#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2021/3/28 0:09
# @Author : cjw
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time

if __name__ == '__main__':
    # 不显示自动化软件控制提示
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-automation'])

    driver = webdriver.Chrome(options=options)
    driver.maximize_window()
    driver.get('https://www.zhipin.com/')
    # driver.find_element_by_xpath('//*[@id="changeCityBox"]/ul/li[1]/a')
    WebDriverWait(driver, 10).until(
        lambda x: x.find_element_by_xpath('//input[contains(@placeholder, "搜索职位")]')). \
        send_keys('python', Keys.ENTER)
    try:
        li = WebDriverWait(driver, 20).until(
            lambda x: x.find_element_by_xpath('//*[@id="main"]/div/div[3]/ul/li'))
        li.find_element_by_xpath('.//span[@class="job-name"]/a').click()
        # 获取浏览器句柄，切换窗口
        driver.switch_to.window(driver.window_handles[-1])
        job_text = driver.find_element_by_xpath('//div[@class="job-sec"]/div[@class="text"]').text
        print(job_text)
        time.sleep(2)
        driver.switch_to.window(driver.window_handles[0])
        time.sleep(2)
        driver.switch_to.frame()
    except Exception as e:
        print(e)
    finally:
        driver.quit()
